<?php
//php  判断周几
function  getWeek($week){
    $weekarray=array("日","一","二","三","四","五","六");
   return  "周".$weekarray[date("w",$week)];
}
 //生成交易事务处理 stype统一采用订单前缀 buid 对方会员id
function transaction($oidprev,$amount = 0,$uid,$info = '',$result = 0,$buid=0 ){
        $oid=$oidprev.date('YmdHis').rand(1,10);
		$rs=M('transaction')->add(array(
		   'oid'=>$oid,
		   'uid'=>$uid,
		   'amount'=>$amount,
		   'info'=>$info,
		   'result'=>$result,
		   'ip'=>get_client_ip(),
		   'addtime'=>time(),
		   'stype'=>$oidprev,
		   'type'=>2
		));
		if($rs){
		    $data['id']=$rs;
		    $data['oid']=$oid;
		}else{
		    $data['id']=0;
		}
		return $data;
 }
 //根据会员ID获取字段
function get_user_name($id,$name='wxusername'){
	return M('zy.user')->where(array('id'=>$id))->getField($name);
}
//获取制定月份的开始时间和结束时间
function mFristAndLast($y = "", $m = ""){
	//if ($y == "") $y = date("Y");
		if ($y == ""){
			$y = date("Y");
		}else{
			$y = date("Y")+$y;
		}
    if ($m == "") $m = date("m");
		$m = sprintf("%02d", intval($m));
		$y = str_pad(intval($y), 4, "0", STR_PAD_RIGHT);
		$m>12 || $m<1 ? $m=1 : $m=$m;
		$firstday = strtotime($y . $m . "01000000");
		$firstdaystr = date("Y-m-01", $firstday);
		$lastday = strtotime(date('Y-m-d 23:59:59', strtotime("$firstdaystr +1 month -1 day")));
		return array("0" => $firstday,"1" => $lastday);

}
//头像获取
function avatar($uid, $size) {
	if($uid==0){
		$data=C('zyb_static_url').'upload/images/'.C('zyb_avatar');
	}else{
		$data=M('zy.user')->where(array('id'=>$uid))->getField('img');
		if(!$data){
			$avatar_size = explode(',', C('zyb_avatar_size'));
			$size = in_array($size, $avatar_size) ? $size : '160';
			$avatar_file =  md5($uid) . "_{$size}.png";
		    $file= WEB_ROOT.'rc/upload/avatar/' . $avatar_file;
		    if (!is_file($file)) {
		        $avatar_file = "default.gif";
		    }
		    $data=C('zyb_static_url').'upload/avatar/'.$avatar_file;
		}

	}
    return  $data;
}
/*转化月份*/
function to_daxie($number){
 $number=substr($number,0,2);
 $arr=array("零","一","二","三","四","五","六","七","八","九");
 if(strlen($number)==1){
  $result=$arr[$number];
  }else{
   if($number==10){
    $result="十";
   }else{
    if($number<20){
    $result="十";
    }else{
    $result=$arr[substr($number,0,1)]."十";
    }
    if(substr($number,1,1)!="0"){
    $result.=$arr[substr($number,1,1)];
    }
   }
 }
 return $result;
}

//获取路径
function BUI($mode,$action,$id,$uid){
	if($uid!='' && $id!=''){
		return U($mode."/".$action,array('id'=>$id,'uid'=>$uid));
	}else if($id!=''){
		return U($mode."/".$action,array('id'=>$id));
	}else if($uid!=''){
		return U($mode."/".$action,array('uid'=>$uid));
	}else{
		return U($mode."/".$action);
	}
}


//获取本地图像
function attach($attach,$type,$ategory) {
    if(!$attach)return 'http://rc.zhiyoumeng.com/static/images/default.png';
    if($ategory==''){$ategory='www';}
	if($attach==strpos($attach,'http://rc.nkidc.cn/')){
    	return str_replace('http://rc.nkidc.cn/','http://rc.zhiyoumeng.com/',$attach);
    }
    if (false === strpos($attach, 'http://')) {
        if($ategory=='www'){
	        //本地附件
	        return C('zyb_static_url').'upload/images/'.$attach;
        }else{
            //七牛空间
            //$qnhttp='zyb_qiniu.qnhttp_'.$ategory;
	        return 'http://7xop19.com2.z0.glb.qiniucdn.com/'.$attach;
        }
    } else {
        //URL链接
        return $attach;
    }
}
function attachrc($attach,$type,$ategory) {
    if(!$attach)return 'http://rc.zhiyoumeng.com/static/images/default.png';

    if($ategory==''){$ategory='www';}
	if($attach==strpos($attach,'http://rc.nkidc.cn/')){
    	return str_replace('http://rc.nkidc.cn/','http://rc.zhiyoumeng.com/',$attach);
    }

    if (false === strpos($attach, 'http://')) {
        if($ategory=='www'){
	        //本地附件
	        return 'http://rc.zhiyoumeng.com/upload/images/'.$attach;
        }else{
            //七牛空间
            //$qnhttp='zyb_qiniu.qnhttp_'.$ategory;
	        return 'http://7xop19.com2.z0.glb.qiniucdn.com/'.$attach;
        }
    } else {
        //URL链接
        return $attach;
    }
}


//判断七牛服务器是否开启
function checkAtegory(){
	if(C('zyb_is_qnserver')==1){
		return C('zyb_qiniu_ategory');
	}else{
		return 'www';
	}
}
/**
 * 标签解析
 * */
function js_minify($options) {
    if(APP_DEBUG){
        $files = explode(',',$options);
        foreach ($files as $val) {
            echo ( '<script type="text/javascript" src="'. $val. '"></script>');
		}
    }else{
    	$statics_url = WEB_ROOT.'static/frontend/';
        $path = $statics_url.'/runjs/' . md5($options.ZYB_RELEASE) . '.js';
        if (!is_file($path)) {
            //静态资源地址
            $files = explode(',',$options);
            $content = '';
            foreach ($files as $val) {
              $val = str_replace('__STATIC__', $statics_url, $val);
			  $val = str_replace('__NSTATIC__',WX_ROOT.'/static/frond/', $val);
              $content.=file_get_contents($val);
            }
            file_put_contents($path,JSMin::minify($content));
        }
        echo ( '<script type="text/javascript" src="'.C('zyb_site_url').'static/frontend/runjs/' . md5($options.ZYB_RELEASE) . '.js"></script>');
    }
}
function getCname($field){
  session_start();
  $uid=intval($_GET['uid']);
 //当前会员是否开通名片
  $user=session('user_info');
  //当为自己时
  if($uid==$user['card']['uid'] || $uid==null){
      if($field=='uid'){
          //返回uid
          return $user['card']['uid'];
      }else{
          //返回名片数据
          return $user['card'];
      }
  }
  $tid=$user['is_vip'];
  //来源id名片
  $cmap['uid']=$uid;
  $cmap['status']=array('gt',0);
  //找到名片表
  $buser=M('zy.card')->where($cmap)->field('uid,team,mobile,wxusername,weixin,wxategory,img,ategory')->find();
  $fid=$buser['uid'];
  if($fid&&$tid){
  //会员与会员
    $data=$buser; //返回前者会员
  }else if($fid&&!$tid){
    //会员与非会员
    $user['card']=$buser;
    session('user_info',$user);
    $data=$buser;  //返回会员
  }else if(!$fid&&!$tid){
  //非会员与非会员
   $data=$user['card'];  //直游盟默认
  }else if(!$fid&&$tid){
    //非会员与会员
    $data=$user['card'];
  }

  if($field=='uid'){
      //返回uid
      return $data['uid'];
  }else{
      //返回名片数据
      return $data;
  }
}
//获取当前页面链接
function get_url(){
    $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
    $php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
    $path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
    $relate_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING'] : $path_info);
    return $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '').$relate_url;
 }
/**截取字段*/
function sysSubStr($String,$Length=25,$Append = true)
{
    if (strlen($String)<=$Length ){return $String; }else
    {
        $I = 0;
        while ($I<$Length)
        {
            $StringTMP = substr($String,$I,1);
            if ( ord($StringTMP) >=224 ){$StringTMP = substr($String,$I,3);$I = $I + 3;}
            elseif( ord($StringTMP) >=192 ){$StringTMP = substr($String,$I,2); $I = $I + 2;}
            else{ $I = $I + 1;}
            $StringLast[] = $StringTMP;
        }
        $StringLast = implode("",$StringLast);
        if($Append){$StringLast= htmlspecialchars($StringLast)."..";}
        return $StringLast;
    }
}
//根据变量获取签证分类
function get_visa($variable,$table='article_cate'){

  return M($table)->where(array('variable'=>$variable))->field('id')->getField('id');
}

 //url获取
function curl_file_get_contents($durl){
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $durl);
   curl_setopt($ch, CURLOPT_TIMEOUT, 50);
   curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
   curl_setopt($ch, CURLOPT_HEADER, FALSE);
   $r = curl_exec($ch);
   curl_close($ch);
   return $r;
 }

function stripslashes_deep($value) {
    if (is_array($value)) {
        $value = array_map('stripslashes_deep', $value);
    } elseif (is_object($value)) {
        $vars = get_object_vars($value);
        foreach ($vars as $key => $data) {
            $value->{$key} = stripslashes_deep($data);
        }
    } else {
        $value = stripslashes($value);
    }
    return $value;
}
function addslashes_deep($value) {
    $value = is_array($value) ? array_map('addslashes_deep', $value) : addslashes($value);
    return $value;
}
/**
* 获取mod 的状态属于status = 1 id字符串 
*/
function get_status($mod_name,$val = 1) {
	$mod = D($mod_name);
	$pk = $mod->getPk();
	$data = $mod->where(array('status'=>$val))->select();
	$str = '';
	if($data){
		foreach($data as $k=>$v){
			$str .= $v[$pk].',';
		}
		$str = substr($str,0,-1);
	}
	return $str;
}
/**
* 获取arr 的状态属于status = 1 id字符串 
*/
function get_arr_status($arr) {
	$str = '';
	if($arr){
		foreach($arr as $k=>$v){
			$str .= $v['id'].',';
		}
		$str = substr($str,0,-1);
	}
	return $str;
}
function get_img_url($img){
	return (strpos($img,'http')===false) ? C('zyb_img_path').$img : $img;
}
function _tmp_write($data,$path=null){
	if(!OPEN_TMP_WRITE){
		return false;
	}
	if($path == null){
		$file_path = $_SERVER['REDIRECT_URL'];
		
	}else{
		$file_path = $path;
	}
	$dir_path = BASE_PATH_ROOT.dirname($file_path);
	if(!file_exists($dir_path)){
		mkdir($dir_path,0777,true);
	}
	$file_path = $dir_path.'/'.basename($file_path);
	file_put_contents($file_path,$data);
}